import { login as apiLogin } from './api';
import type { ILoginParams } from './index.d';
import { Dispatch } from 'redux';
import { updateToken, updateUserInfo } from '@/store/user';

/**
 * 登录
*/
export const login = (params: ILoginParams) => {
  return async (dispatch: Dispatch): Promise<void> => {
    try {
      const { data } = await apiLogin(params)

      // 更新token
      dispatch(updateToken(data.token))

      // 更新用户信息
      dispatch(updateUserInfo(data))
    } catch {}
  }
}
